package org.flyby.api.persistence;

import javax.persistence.*;

@Entity
@Table(name = "code_table_data")
@NamedQueries({
        @NamedQuery(name = "CodeTableData.findByTableHeaderCodeAndItemCode",
                query = " select ct " +
                        " from CodeTableData ct " +
                        " where ct.codeTableHeader.tableHeaderCode =:tableHeaderCode " +
                        " and ct.itemCode = :itemCode ")
})
public class CodeTableData implements BaseEntity {

    public final static String HEADER_CART_STATUS_CODE = "1";
    public final static String DATA_CART_STATUS_START_SHOPPING = "1";
    public final static String DATA_CART_STATUS_WAIT_FOR_PAYMENT = "2";
    public final static String DATA_CART_STATUS_PAYMENT_RECEIVED = "3";
    public final static String DATA_CART_STATUS_PAYMENT_FAILED = "4";
    public static final String DATA_CART_SELECTED = "5";

    @Id
    @Access(AccessType.PROPERTY)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CODE_TABLE_DATA_ID", nullable = false)
    private int id;

    @Column(name = "ITEM_CODE", nullable = false, length = 4)
    private String itemCode;

    @Column(name = "ITEM_VALUE", nullable = false, length = 22)
    private String itemValue;

    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "TABLE_HEADER_ID", referencedColumnName = "TABLE_HEADER_ID", nullable = false)
    private CodeTableHeader codeTableHeader;

    @Override
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getItemCode() {
        return itemCode;
    }

    public void setItemCode(String itemCode) {
        this.itemCode = itemCode;
    }

    public String getItemValue() {
        return itemValue;
    }

    public void setItemValue(String itemValue) {
        this.itemValue = itemValue;
    }

    public CodeTableHeader getCodeTableHeader() {
        return codeTableHeader;
    }

    public void setCodeTableHeader(CodeTableHeader codeTableHeader) {
        this.codeTableHeader = codeTableHeader;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CodeTableData that = (CodeTableData) o;

        if (id != that.id) return false;
        if (codeTableHeader != null ? !codeTableHeader.equals(that.codeTableHeader) : that.codeTableHeader != null)
            return false;
        if (itemCode != null ? !itemCode.equals(that.itemCode) : that.itemCode != null) return false;
        if (itemValue != null ? !itemValue.equals(that.itemValue) : that.itemValue != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (itemCode != null ? itemCode.hashCode() : 0);
        result = 31 * result + (codeTableHeader != null ? codeTableHeader.hashCode() : 0);
        return result;
    }
}
